//
//  FSBaseViewController.m
//  FSScrollViewNestTableViewDemo
//
//  Created by huim on 2017/5/23.
//  Copyright © 2017年 fengshun. All rights reserved.
//

#import "ShopViewController.h"
#import "NestedRollingTableView.h"
#import "FSScrollContentView.h"
#import "NestedrollingCell.h"
#import "NestedrollingChildController.h"
#import "NestedrollingBottomTableViewCell.h"

@interface ShopViewController ()<UITableViewDelegate,UITableViewDataSource,NestedRollingPageContenViewDelegate,FSSegmentTitleViewDelegate>
@property (nonatomic, strong) NestedRollingTableView *tableView;
@property (nonatomic, strong) NestedrollingBottomTableViewCell *contentCell;
@property (nonatomic, strong) NestedRollingSegmentTitleView *titleView;
@property (nonatomic, assign) BOOL canScroll;
@property (nonatomic, strong)NSMutableArray *sortTitles;
@property (nonatomic, strong)NSMutableArray *sortTitlesID;
@property (nonatomic, strong)NSMutableArray *bannerList;
@end

@implementation ShopViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setUI];

}

- (void)setUI{
    self.canScroll = YES;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(insertRowAtTop)];
    self.tableView.mj_header = header;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(20);
        make.bottom.mas_offset(49-FIT49IPHONEX +20);
    }];
    self.sortTitles = @[].mutableCopy;
    self.sortTitlesID = @[].mutableCopy;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus) name:@"leaveTop" object:nil];
}
- (void)insertRowAtTop
{
    self.sortTitles = @[@"健康服务",@"营养健康",@"健康饮食"].mutableCopy;
    self.contentCell.currentTagStr = self.sortTitles[self.titleView.selectIndex];
    self.contentCell.isRefresh = YES;
    [self.tableView.mj_header endRefreshing];
//    [self.tableView.pullToRefreshView stopAnimating];
}

#pragma mark notify
- (void)changeScrollStatus//改变主视图的状态
{
    self.canScroll = YES;
    self.contentCell.cellCanScroll = NO;
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 223 + FIT44IPHONEX - 30;
        }
        return 50;
    }
    return CGRectGetHeight(self.view.bounds);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.titleView = [[NestedRollingSegmentTitleView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 50) titles:self.sortTitles delegate:self indicatorType:FSIndicatorTypeEqualTitle];
    self.titleView.indicatorColor = [UIColor colorWithHexString:@"#2180B6"];
    self.titleView.titleSelectColor =[UIColor colorWithHexString:@"#2180B6"];
    self.titleView.titleNormalColor =[UIColor colorWithHexString:@"#333333"];
    self.titleView.backgroundColor = [UIColor whiteColor];
    return self.titleView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        _contentCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!_contentCell) {
            _contentCell = [[NestedrollingBottomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        NSMutableArray *contentVCs = [NSMutableArray array];
        if (self.sortTitles.count == 0) {
            NestedrollingChildController *vc = [[NestedrollingChildController alloc]init];
            vc.title =  @"";
            vc.str = @"";
            [contentVCs addObject:vc];
        }else{
            for (int i = 0; i < self.sortTitles.count; i++) {
                NestedrollingChildController *vc = [[NestedrollingChildController alloc]init];
                vc.title =  self.sortTitles[i];
                vc.str = self.sortTitlesID[i];
                [contentVCs addObject:vc];
            }
        }
        
        _contentCell.viewControllers = contentVCs;
        _contentCell.pageContentView = [[NestedRollingPageContentView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - FIT64IPHONEX + 49 - FIT49IPHONEX - 100) childVCs:contentVCs parentVC:self delegate:self];
        [_contentCell.contentView addSubview:_contentCell.pageContentView];
//        _contentCell.contentView.backgroundColor = [ColorManager WhiteColor];
        _contentCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return _contentCell;
    }
    if (indexPath.row == 0) {
        NestedrollingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NestedrollingCellIdentify"];
        if (!cell) {
            cell = [[NestedrollingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NestedrollingCellIdentify"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}

#pragma mark FSSegmentTitleViewDelegate
- (void)FSContenViewDidEndDecelerating:(NestedRollingPageContentView *)contentView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex
{
    self.titleView.selectIndex = endIndex;
    _tableView.scrollEnabled = YES;//此处其实是监测scrollview滚动，pageView滚动结束主tableview可以滑动，或者通过手势监听或者kvo，这里只是提供一种实现方式
}


- (void)nestedRollingPageContenViewDidEndDecelerating:(NestedRollingPageContentView *)contentView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex{
    self.contentCell.pageContentView.contentViewCurrentIndex = endIndex;
}
- (void)nestedRollingPageContenViewDidScroll:(NestedRollingPageContentView *)contentView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex progress:(CGFloat)progress{
    _tableView.scrollEnabled = NO;//pageView开始滚动主tableview禁止滑动
}


#pragma mark UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    CGFloat bottomCellOffset = [_tableView rectForSection:1].origin.y - 64;
    if (scrollView.contentOffset.y >= bottomCellOffset) {
        scrollView.contentOffset = CGPointMake(0, bottomCellOffset);
        if (self.canScroll) {
            self.canScroll = NO;
            self.contentCell.cellCanScroll = YES;
        }
    }else{
        if (!self.canScroll) {//子视图没到顶部
            scrollView.contentOffset = CGPointMake(0, bottomCellOffset);
        }
    }
    self.tableView.showsVerticalScrollIndicator = _canScroll?YES:NO;
}

#pragma mark LazyLoad
- (NestedRollingTableView *)tableView
{
    if (!_tableView) {
        _tableView = [[NestedRollingTableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.bounces = NO;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
